package com.xiao.controller;

import com.xiao.annotation.SystemLog;
import com.xiao.domain.constants.ArticleConstants;
import com.xiao.domain.ResponseResult;
import com.xiao.domain.pojo.Article;
import com.xiao.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "文章控制层")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    // --------------------------- mybatis-Test--【Start】---------------------

    @GetMapping("/getAllList")
    @ApiOperation("返回 所有文章列表")
    @SystemLog(businessName = "返回 所有文章列表")
    public List<Article> getList(){
        return articleService.list();
    }
    // --------------------------- mybatis-Test--【End】---------------------


    // 返回热门文章列表
    @GetMapping("/hotArticleList")
    @ApiOperation("返回 热门文章列表; 数量限制："+ ArticleConstants.HOT_VIEW_COUNT)
    @SystemLog(businessName = "返回 热门文章列表; 数量限制："+ ArticleConstants.HOT_VIEW_COUNT)
    public ResponseResult hosArticleList(){
        // 封装成 ResponseResult 统一响应后返回
        ResponseResult result = articleService.hosArticleList();
        return result;
    }


    // 他不是用 RestFul 风格 默认是 ?+& 拼接
    @GetMapping("/articleList") // /{pageNum}/{pageSize}/{categoryId}
    @ApiOperation("可以根据分类Id: 返回文章分页列表,默认返回所有")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "第几页"),
            @ApiImplicitParam(name = "pageSize",value = "页面大小"),
            @ApiImplicitParam(name = "categoryId",value = "分类ID"),
    })
    public ResponseResult articleList(/*@PathVariable("pageNum") */Integer pageNum, // 因为是 get 请求，因此可以直接接收参数
                                      /*@PathVariable("pageSize") */Integer pageSize,
                                      /*@PathVariable("categoryId")*/ Long categoryId){
        return articleService.articleList(pageNum,pageSize,categoryId);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据文章ID,返回文章;")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "文章ID")
    })
    public ResponseResult getArticleDetailsById(@PathVariable("id") Long id){// 因为这里虽然是 get 请求但是 是 ResultFul 风格 所以需要 @PathVariable
        return articleService.getArticleDetailsById(id);
    }

    @PutMapping("/updateViewCount/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "需要增量的文章ID")
    })
    @ApiOperation("根据文章ID,增量浏览量")
    public ResponseResult updateViewCount(@PathVariable("id") Long id){

        ResponseResult result = articleService.updateViewCount(id);
        return result;
    }

}